package com.emc.atmos.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AtmosExMeta {
	
	private String oid = null;
	private String url = null;
	private String username = null;
	private String password = null;
	private String [] tagStringArray;
	private int maxDbCols = 8;
	
	public AtmosExMeta(String local_oid, String local_url, String local_username, String local_password, String [] local_tagStringArray)
	{
		this.oid = local_oid;
		this.url = local_url;
		this.username = local_username;
		this.password = local_username;
		this.tagStringArray = local_tagStringArray;
	}
	

	public int UploadExMeta()
	{
		//status 1 is bad
		int status=1;
		
		//init local temp array to same size as inbound array. and fill with nulls. Need to change to max columns...
		//String [] tempTagAr = new String[this.tagStringArray.length];
		//for (int i=0;i<tempTagAr.length;i++)
		String [] tempTagAr = new String[this.maxDbCols];

		//there is a more efficient way.  don't need to worry about it now
		for (int i=0;i<this.maxDbCols;i++)

		{
			//tempTagAr[i] = null;
			tempTagAr[i] = "iamnull";

		}
		
		
		//set the inbound values in the temparray
		for (int i=0;i<this.tagStringArray.length;i++)
		{
			tempTagAr[i] = this.tagStringArray[i];
		}
		
		
		//open the database driver
		try {
			 
			Class.forName("org.postgresql.Driver");
			status=0;
 
		} catch (ClassNotFoundException e) {
 
			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
			status=1;
 
		}
		
		// connect to DB and do work
		Connection connection = null;
		if (status == 0)
		{
			try {
	 
				//connection = DriverManager.getConnection(
					//	"jdbc:postgresql://egd40001.noe.cjis.fbi.gov:5432/atmosmetadb", "atmosmeta",
						//"atmosmeta");
				
				//try to connect to Extended Metadata database
				try{
					connection = DriverManager.getConnection(this.url, this.username, this.password);
				} catch (SQLException e) {
					System.out.println("problem connecting to database with " + url);
					System.out.println(e.getMessage());
				}
				
				//set prepared statement.  Need to dynamically build string to tolerate arrying array lengths
				PreparedStatement stmt = connection.prepareStatement("INSERT into atmosmeta.atmosmeta VALUES (?,?,?,?,?,?,?,?,?)");
				
				//put oid and metadata information in stmt
				stmt.setString(1, this.oid);
				for(int i=0;i<tempTagAr.length;i++)
				{
					if (tempTagAr[i].equals("iamnull"))
					{
						stmt.setNull(i+2, java.sql.Types.VARCHAR);
					} else {
						stmt.setString(i+2, tempTagAr[i]);
					}
				}
				
				stmt.executeUpdate();
				
				//status 0 is good
				status=0;
				
			} catch (SQLException e) {
	 
				System.out.println("Connection Failed! Check output console");
				e.printStackTrace();
				 
			}
		
			
		}	
		return status;
	}
}
